<?php
class RecordAction extends Action {
	public function index() {
		$this->assign ( 'title', '羽毛球积分赛 - 比赛记录' );
		
		// 判断用户登录信息
		$this->assign ( 'admin', 'none' );
		$this->assign ( 'newrecord', 'none' );
		if (session ( 'username' )) {
			$this->assign ( 'loginuser', session ( 'username' ) );
			$this->assign ( 'userscore', session ( 'userscore' ) );
			$this->assign ( 'usermatchcount', session ( 'usermatchcount' ) );
			$this->assign ( 'loginusercolor', get_color ( session ( 'usergender' ) ) );
			
			if (intval ( session ( 'userlevel' ) ) < 1) {
				$this->assign ( 'userstatus', '（未认证）' );
			} else {
				$this->assign ( 'userstatus', '' );
				$this->assign ( 'newrecord', 'inline' );
			}
			
			if (intval ( session ( 'userlevel' ) ) > 1) {
				$this->assign ( 'admin', 'inline' );
			}
			
			$this->assign ( 'logoffblock', 'inline' );
			$this->assign ( 'loginblock', 'none' );
		} else {
			$this->assign ( 'logoffblock', 'none' );
			$this->assign ( 'loginblock', 'inline' );
		}
		
		$users = M ( 'Users' );
		$user_data = $users->field ( 'user_id,username,gender' )->where ( 'level > 0' )->select ();
		for($i = 0; $i < count ( $user_data ); $i ++) {
			$user_index [$user_data [$i] ['user_id']] = $user_data [$i] ['username'];
			$user_gender [$user_data [$i] ['user_id']] = $user_data [$i] ['gender'];
		}
		$this->assign ( 'userlist', $user_data );
		
		$matches = M ( 'MatchRecords' );
		$data = $matches->order ( 'match_date desc,match_id desc' )->select ();
		for($i = 0; $i < count ( $data ); $i ++) {
			if ($data [$i] ['match_type'] == 'single') {
				$data [$i] ['match_type'] = '单打';
			} else if ($data [$i] ['match_type'] == 'double') {
				$data [$i] ['match_type'] = '双打';
			} else if ($data [$i] ['match_type'] == 'small') {
				$data [$i] ['match_type'] = '小球';
			} else {
				$this->error ( '非法数据！match_type=' . $data [$i] ['match_type'] );
			}
			
			if ($data [$i] ['match_rule'] == '11scores') {
				$data [$i] ['match_rule'] = '11分制';
			} else if ($data [$i] ['match_rule'] == '21scores') {
				$data [$i] ['match_rule'] = '21分制';
			} else {
				$this->error ( '非法数据！match_rule=' . $data [$i] ['match_rule'] );
			}
			
			$data [$i] ['color1'] = get_color ( $user_gender [$data [$i] ['pros_player1']] );
			$data [$i] ['color2'] = get_color ( $user_gender [$data [$i] ['pros_player2']] );
			$data [$i] ['color3'] = get_color ( $user_gender [$data [$i] ['cons_player1']] );
			$data [$i] ['color4'] = get_color ( $user_gender [$data [$i] ['cons_player2']] );
			$data [$i] ['color5'] = get_color ( $user_gender [$data [$i] ['recorder']] );
			
			$data [$i] ['pros_player1'] = $user_index [$data [$i] ['pros_player1']];
			$data [$i] ['pros_player2'] = $user_index [$data [$i] ['pros_player2']];
			$data [$i] ['cons_player1'] = $user_index [$data [$i] ['cons_player1']];
			$data [$i] ['cons_player2'] = $user_index [$data [$i] ['cons_player2']];
			$data [$i] ['recorder'] = $user_index [$data [$i] ['recorder']];
			
			$data [$i] ['score'] = $data [$i] ['pros_score'] . ':' . $data [$i] ['cons_score'];
			$data [$i] ['match_coef'] = sprintf ( "%.1lf", round ( $data [$i] ['match_coef'], 1 ) );
			
			if ($data [$i] ['match_type'] != '双打') {
				$data [$i] ['detail'] = $data [$i] ['pros_player1'] . '(' . $data [$i] ['ori_score_pros_player1'] . ') VS ' . $data [$i] ['cons_player1'] . '(' . $data [$i] ['ori_score_cons_player1'] . ')';
				$data [$i] ['detail'] .= '&#13--------------------';
				if ($data [$i] ['var_score_pros_player1'] >= 0) {
					$varA = '+' . $data [$i] ['var_score_pros_player1'];
				} else {
					$varA = $data [$i] ['var_score_pros_player1'];
				}
				if ($data [$i] ['var_score_cons_player1'] >= 0) {
					$varB = '+' . $data [$i] ['var_score_cons_player1'];
				} else {
					$varB = $data [$i] ['var_score_cons_player1'];
				}
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['pros_player1'] . ': ' . $varA;
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['cons_player1'] . ': ' . $varB;
			} else {
				$data [$i] ['detail'] = $data [$i] ['pros_player1'] . '(' . $data [$i] ['ori_score_pros_player1'] . ') ' . $data [$i] ['pros_player2'] . '(' . $data [$i] ['ori_score_pros_player2'] . ') VS ' . $data [$i] ['cons_player1'] . '(' . $data [$i] ['ori_score_cons_player1'] . ') ' . $data [$i] ['cons_player2'] . '(' . $data [$i] ['ori_score_cons_player2'] . ')';
				$data [$i] ['detail'] .= '&#13--------------------';
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['pros_player1'] . ': ' . $data [$i] ['var_score_pros_player1'];
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['pros_player2'] . ': ' . $data [$i] ['var_score_pros_player2'];
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['cons_player1'] . ': ' . $data [$i] ['var_score_cons_player1'];
				$data [$i] ['detail'] .= '&#13' . $data [$i] ['cons_player2'] . ': ' . $data [$i] ['var_score_cons_player2'];
			}
		}
		$this->assign ( 'recordlist', $data );
		
		$this->display ();
	}
	
	public function add() {
		if (! is_score_valid ( $_POST ['pros_score'], $_POST ['cons_score'] )) {
			$this->error ( '比分不合法' );
		}
		if ($_POST ['match_type'] == 'double') {
			if ($_POST ['pros_player2'] == '' || $_POST ['cons_player2'] == '') {
				$this->error ( '双打比赛需填入正方二和反方二' );
			}
			if ($_POST ['pros_player1'] == $_POST ['pros_player2'] || $_POST ['pros_player1'] == $_POST ['cons_player1'] || $_POST ['pros_player1'] == $_POST ['cons_player2'] || $_POST ['pros_player2'] == $_POST ['cons_player1'] || $_POST ['pros_player2'] == $_POST ['cons_player2'] || $_POST ['cons_player1'] == $_POST ['cons_player2']) {
				$this->error ( '同一选手在一场比赛中只能以一个身份出现' );
			}
		} else {
			if ($_POST ['pros_player2'] != '' || $_POST ['cons_player2'] != '') {
				$this->error ( '单打比赛不需填入正方二和反方二' );
			}
			if ($_POST ['pros_player1'] == $_POST ['cons_player1']) {
				$this->error ( '同一选手在一场比赛中只能以一个身份出现' );
			}
		}
		
		$records = D ( 'MatchRecords' );
		
		if (! $records->create ()) {
			$this->error ( $records->getError () );
		}
		
		$records->match_date = date ( "Y-m-d" );
		$records->recorder = session ( 'userid' );
		
		if (! $records->add ()) {
			$this->error ( $records->getError () );
		}
		
		$this->redirect("Admin/recalcmatch");
	}
}